package com.zhudi.develop_demo_as.ui.fragment;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhudi.develop.util.ZhudiFileUtil;
import com.zhudi.develop.util.ZhudiToastSingle;
import com.zhudi.develop.util.ZhudiVersionUtil;
import com.zhudi.develop_demo_as.BaseFragment;
import com.zhudi.develop_demo_as.MainApplication;
import com.zhudi.develop_demo_as.R;

import butterknife.Bind;
import butterknife.OnClick;

public class MoreFragment extends BaseFragment {
	private ProgressDialog pd = null;
	@Bind(R.id.tvTitle)
	TextView tvTitle;
	@Bind(R.id.sv)
	View sv;
	@Bind(R.id.settings_cache_size)
	TextView tvCacheSize;
	@Bind(R.id.settings_version_name)
	TextView tvVersionName;

	@Override
	protected int getLayoutView() {
		return R.layout.fragment_more;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView(view);
	}

	private void initView(View view) {
		tvTitle.setText("更多");
		sv.setOverScrollMode(view.OVER_SCROLL_NEVER);
		tvVersionName.setText("V" + ZhudiVersionUtil.getPackageVersion(getActivity()));
	}


	@OnClick(R.id.settings_clear_cache)
	public void clearCache(){
		 new clearCacheTask().execute();
	}

	/**清空缓存 */
	private  class clearCacheTask extends AsyncTask<Void,Void,Boolean>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pd = new ProgressDialog(getActivity());
			pd.setMessage(getString(R.string.wait));
			pd.setCanceledOnTouchOutside(false);
			pd.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			return ZhudiFileUtil.delete(getActivity().getCacheDir());
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			pd.dismiss();
			if (result){
				tvCacheSize.setText("0B");
			} else {
				ZhudiToastSingle.showToast(MainApplication.getInstance(),"清空缓存失败", Toast.LENGTH_SHORT,-1);
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		tvCacheSize.setText(ZhudiFileUtil.getAutoFileOrFilesSize(getActivity().getCacheDir().getPath()));
	}
}
